# -*- coding: utf-8 -*-

"""
Module implementing CCommSetting.
"""

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QDialog

from Ui_CommSetting import Ui_Dialog

import serial
import serial.tools.list_ports

class CCommSetting(QDialog, Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(CCommSetting, self).__init__(parent)
        self.setupUi(self)
        
        self.InitPortList()
            
        self.cboBaud.setCurrentIndex(4)
        self.cboStopBit.setCurrentIndex(1)
        self.cboParity.setCurrentIndex(0)
        
        self.stopBitList = [serial.STOPBITS_ONE,  serial.STOPBITS_TWO]
        self.parityList = [serial.PARITY_NONE, serial.PARITY_ODD,  serial.PARITY_EVEN] 
        
        self.port = self.cboPort.currentText()
        self.baud = int(self.cboBaud.currentText())
        self.stopbit = self.stopBitList[self.cboStopBit.currentIndex()]
        self.parity = self.parityList[self.cboParity.currentIndex()]
        

    
    @pyqtSlot()
    def on_btnOK_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        self.accept()
        
    def Show(self):
        
        self.InitPortList()
        self.show()
    
    @pyqtSlot(int)
    def on_cboParity_activated(self, index):
        """
        Slot documentation goes here.
        
        @param index DESCRIPTION
        @type int
        """
        # TODO: not implemented yet
        self.parity = self.parityList[index]
    
    @pyqtSlot(int)
    def on_cboStopBit_activated(self, index):
        """
        Slot documentation goes here.
        
        @param index DESCRIPTION
        @type int
        """
        # TODO: not implemented yet
        self.stopbit = self.stopBitList[index]
    
   
    @pyqtSlot(str)
    def on_cboPort_activated(self, p0):
        """
        Slot documentation goes here.
        
        @param p0 DESCRIPTION
        @type str
        """
        # TODO: not implemented yet
        self.port = p0
    
    @pyqtSlot(str)
    def on_cboBaud_activated(self, p0):
        """
        Slot documentation goes here.
        
        @param p0 DESCRIPTION
        @type str
        """
        # TODO: not implemented yet
        self.baud = int(p0)

    def InitPortList(self):
        
        # Get port list and add to combo box
        portList = list(serial.tools.list_ports.comports())
        
        self.cboPort.clear()
        
        for port in portList:
            self.cboPort.addItem(port[0])        
